#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>

#include "mail.h"
#include "log.h"

int send_mail(char *_to, char *_subject, char *_content)
{
	log_info("begin send_mail.");

	if (_to == NULL ||_subject == NULL || _content == NULL) {
		return 1;
	}
	
	char to[MAIL_TO_MAX_LEN], subject[MAIL_SUBJECT_MAX_LEN], content[MAIL_CONTENT_MAX_LEN];	     
	strcpy(to, _to);
	to[MAIL_TO_MAX_LEN - 1] = '\0';
	strcpy(subject, _subject);
	subject[MAIL_SUBJECT_MAX_LEN - 1] = '\0';
	strcpy(content, _content);
	content[MAIL_CONTENT_MAX_LEN - 1] = '\0';
	
	char cmd[MAIL_CMD_MAX_LEN];
	sprintf(cmd, "echo \"%s\" | %s -s \"%s\" %s", content, MAIL_PROGRAM, subject, to);
	cmd[MAIL_CMD_MAX_LEN - 1] = '\0';

	int ret = 0;
	ret = system(cmd);

	return ret;
}
